package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.VoteItem;
import com.ericom.service.GenericManager;

public class VoteItemAction extends BaseAction {

	private GenericManager<VoteItem, Long> voteItemManager;
	private List voteItems;
	private VoteItem voteItem;
	private Long id;

	public VoteItem getVoteItem() {
		return voteItem;
	}

	public void setVoteItem(VoteItem voteItem) {
		this.voteItem = voteItem;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setVoteItemManager(
			GenericManager<VoteItem, Long> voteItemManager) {
		this.voteItemManager = voteItemManager;
	}

	public String list() {
		voteItems = voteItemManager.getAll();
		return SUCCESS;
	}

	public List getVoteItems() {
		return voteItems;
	}

	public String delete() {
		voteItemManager.remove(voteItem.getId());
		this.saveMessage(getText("voteItem.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			voteItem = voteItemManager.get(id);
		} else {
			voteItem = new VoteItem();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (voteItem.getId() == null);
		voteItem = voteItemManager.save(voteItem);

		String key = (isNew) ? "voteItem.added" : "voteItem.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
